﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;

namespace MusicLibraryGUI
{
    public partial class Form1 : Form
    {
        ILoggerFactory loggerFactory = new FileLoggerFactory();
        ILogger logger;

        IAudioRecordFactory podcastFactory = new PodcastFactory();
        IMusicTrackFactory[] musicTrackFactory = { new SongFactory(), new ClassicFactory() };

        PlayList playList = new PlayList();

        private void updateTotalDuration()
        {
            labelTotalDuration.Text = "Total Duration: " + playList.CalculateTime().ToString();
        }

        private void updatePlayList()
        {
            listBoxPlaylist.SelectedIndex = -1;
            listBoxPlaylist.Items.Clear();
            foreach (AudioRecord rec in playList.Tracks())
            {
                listBoxPlaylist.Items.Add(rec.Info());
            }
        }
        
        public Form1()
        {
            InitializeComponent();
            updateTotalDuration();
            updatePlayList();
            logger = loggerFactory.CreateLogger("test.log");
            logger.Log("Starting initialization");
        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            playList.ClearList();
            logger.Log("Playlist cleared");
            updateTotalDuration();
            updatePlayList();
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            int index = listBoxPlaylist.SelectedIndex;
            if (index != -1)
            {
                logger.Log("Deleting Audiro Record: #" + index.ToString() + " - " + ((AudioRecord)playList.Tracks()[index]).Info());
                playList.RemoveTrack(index);
                logger.Log("Audio Record deleted");
                updateTotalDuration();
                updatePlayList();
            }
        }

        private void comboBoxARType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(comboBoxARType.SelectedIndex == 0)
            {
                comboBoxMTType.Enabled = true;
            }
            else if (comboBoxARType.SelectedIndex == 1)
            {
                comboBoxMTType.Enabled = false;
            }
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            string artist = textBoxArtist.Text;
            string title = textBoxTitle.Text;
            double duration = double.Parse(textBoxDuration.Text);
            if (comboBoxARType.SelectedIndex == 0)
            {
                if (comboBoxMTType.SelectedIndex == 0)
                {
                    AudioRecord rec = musicTrackFactory[0].CreateMusicTrack(artist, title, duration);
                    playList.AddTtrack(rec);
                    logger.Log("Added to playlist: " + rec.Info());
                }
                else if (comboBoxMTType.SelectedIndex == 1)
                {
                    AudioRecord rec = musicTrackFactory[1].CreateMusicTrack(artist, title, duration);
                    playList.AddTtrack(rec);
                    logger.Log("Added to playlist: " + rec.Info());
                }
            }
            else if (comboBoxARType.SelectedIndex == 1)
            {
                AudioRecord rec = podcastFactory.CreateRecord(artist, title, duration);
                playList.AddTtrack(rec);
                logger.Log("Added to playlist: " + rec.Info());
            }
            updateTotalDuration();
            updatePlayList();
        }

    }
}
